﻿using System;
using System.Collections.Generic;
using IrrlichtLime;
using IrrlichtLime.Core;
using Timed;
using IrrlichtLime.Scene;
using IrrlichtLime.Video;
using LuaInterface;

namespace Iso
{
    public class PositionTrigger
    {
        public Vector3Df position;
        public float size;
        public string function;
        public int type; // 0 = On Enter  1 = Repeated  2 = OneShot

        bool enter = false;

        public PositionTrigger(Vector3Df position, float size, string function, int type)
        {
            this.position = position;
            this.size = size;
            this.function = function;
            this.type = type;
        }

        public int Check(Vector3Df pos)
        {
            float halfsize = size / 2;
            if ((pos.X < position.X + halfsize && pos.X > position.X - halfsize) &&
                (pos.Y < position.Y + halfsize && pos.Y > position.Y - halfsize) &&
                (pos.Z < position.Z + halfsize && pos.Z > position.Z - halfsize))
            {
                if (type == 1)
                {
                    Engine.lua.GetFunction(function).Call(pos);
                    return 1;
                }
                else if (type == 0 && !enter)
                {
                    Engine.lua.GetFunction(function).Call(pos);
                    enter = true;
                    return 1;
                }
                else if (type == 2)
                {
                    Engine.lua.GetFunction(function).Call(pos);
                    return 2;
                }
                else
                    return 0;
            }
            else
            {
                enter = false;
                return 0;
            }
        }
    }
}
